package com.atguigu.acl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.atguigu.acl.entity.*;
import com.atguigu.acl.mapper.RoleMapper;
import com.atguigu.acl.mapper.RoleMenuMapper;
import com.atguigu.acl.mapper.RolePermissionMapper;
import com.atguigu.acl.mapper.RoleResourceMapper;
import com.atguigu.acl.service.RoleService;
import com.atguigu.utils.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author smday
 * @since 2020-08-10
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Override
    public int create(Role role) {
        role.setAdminCount(0);
        return baseMapper.insert(role);
    }

    @Override
    public int update(Long id, Role role) {
        role.setId(id);
        return baseMapper.updateById(role);
    }

    @Override
    public int delete(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids);
    }

    @Override
    public List<Permission> getPermissionList(Long roleId) {
        return rolePermissionMapper.getPermissionList(roleId);
    }

    @Override
    public int updatePermission(Long roleId, List<Long> permissionIds) {

        //先删除原有的关系
        QueryWrapper<RolePermission> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        rolePermissionMapper.delete(wrapper);

        List<RolePermission> rolePermissionList = new ArrayList<>();
        RolePermission rolePermission;
        for (Long permissionId : permissionIds) {
            rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        return rolePermissionMapper.insertList(rolePermissionList);
    }

    @Override
    public List<Role> list() {
        return baseMapper.selectList(null);
    }

    @Override
    public Map<String, Object> list(String keyword, Integer pageSize, Integer pageNum) {

        Page<Role> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        if (!StrUtil.isEmpty(keyword)) {
            wrapper.like("name", keyword );
        }
        baseMapper.selectPage(page, wrapper);
        return PageUtils.convertPageToMap(page);
    }

    @Override
    public List<Menu> getMenuList(Long adminId) {
        return roleMenuMapper.getMenuList(adminId);
    }

    @Override
    public List<Menu> listMenu(Long roleId) {
        return roleMenuMapper.getMenuListByRoleId(roleId);
    }

    @Override
    public List<Resource> listResource(Long roleId) {
        return roleResourceMapper.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {

        //删除原先关系
        QueryWrapper<RoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleMenuMapper.delete(wrapper);

        List<RoleMenu> roleMenuList = new ArrayList<>();
        RoleMenu roleMenu;
        for (Long menuId : menuIds) {
            roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuList.add(roleMenu);
        }
        return roleMenuMapper.insertList(roleMenuList);
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //删除原先关系
        QueryWrapper<RoleResource> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        roleResourceMapper.delete(wrapper);

        List<RoleResource> RoleResourceList = new ArrayList<>();
        RoleResource roleResource;
        for (Long resourceId : resourceIds) {
            roleResource = new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resourceId);
            RoleResourceList.add(roleResource);
        }
        return roleResourceMapper.insertList(RoleResourceList);
    }
}
